import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../widgets/platform/adaptive_card.dart';

/// 知识库页面
class KnowledgeBasePage extends ConsumerStatefulWidget {
  const KnowledgeBasePage({super.key});

  @override
  ConsumerState<KnowledgeBasePage> createState() => _KnowledgeBasePageState();
}

class _KnowledgeBasePageState extends ConsumerState<KnowledgeBasePage> {
  // 模拟知识库数据
  final List<Map<String, dynamic>> _knowledgeItems = [
    {
      'id': '1',
      'title': '需求分析方法论',
      'category': '需求管理',
      'author': '张伟',
      'lastUpdated': '2023-10-01',
      'views': 128,
    },
    {
      'id': '2',
      'title': '数据血缘图谱使用指南',
      'category': '数据治理',
      'author': '李明',
      'lastUpdated': '2023-09-28',
      'views': 95,
    },
    {
      'id': '3',
      'title': '项目管理最佳实践',
      'category': '项目管理',
      'author': '王芳',
      'lastUpdated': '2023-09-25',
      'views': 210,
    },
    {
      'id': '4',
      'title': '待办任务优先级评估标准',
      'category': '任务管理',
      'author': '赵强',
      'lastUpdated': '2023-10-02',
      'views': 76,
    },
    {
      'id': '5',
      'title': '协作空间使用说明',
      'category': '团队协作',
      'author': '孙磊',
      'lastUpdated': '2023-09-30',
      'views': 142,
    },
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const SelectableText('知识库'),
        actions: [
          IconButton(
            icon: const Icon(Icons.search),
            onPressed: () {
              // 搜索功能
            },
          ),
          IconButton(
            icon: const Icon(Icons.filter_list),
            onPressed: () {
              // 筛选功能
            },
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 搜索和筛选区域
            _buildSearchAndFilter(),
            const SizedBox(height: 24),
            // 知识库内容
            Expanded(
              child: _buildKnowledgeList(),
            ),
          ],
        ),
      ),
    );
  }

  // 构建搜索和筛选区域
  Widget _buildSearchAndFilter() {
    return Column(
      children: [
        TextField(
          decoration: InputDecoration(
            hintText: '搜索知识库...',
            prefixIcon: const Icon(Icons.search),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
            ),
            contentPadding: const EdgeInsets.symmetric(
              horizontal: 16,
              vertical: 12,
            ),
          ),
        ),
        const SizedBox(height: 16),
        Wrap(
          spacing: 8,
          runSpacing: 8,
          children: [
            FilterChip(
              label: const SelectableText('全部'),
              selected: true,
              onSelected: (selected) {},
            ),
            FilterChip(
              label: const SelectableText('需求管理'),
              selected: false,
              onSelected: (selected) {},
            ),
            FilterChip(
              label: const SelectableText('数据治理'),
              selected: false,
              onSelected: (selected) {},
            ),
            FilterChip(
              label: const SelectableText('项目管理'),
              selected: false,
              onSelected: (selected) {},
            ),
            FilterChip(
              label: const SelectableText('任务管理'),
              selected: false,
              onSelected: (selected) {},
            ),
            FilterChip(
              label: const SelectableText('团队协作'),
              selected: false,
              onSelected: (selected) {},
            ),
          ],
        ),
      ],
    );
  }

  // 构建知识库列表
  Widget _buildKnowledgeList() {
    return ListView.builder(
      itemCount: _knowledgeItems.length,
      itemBuilder: (context, index) {
        final item = _knowledgeItems[index];
        return _buildKnowledgeItem(item);
      },
    );
  }

  // 构建知识库项
  Widget _buildKnowledgeItem(Map<String, dynamic> item) {
    return AdaptiveCard(
      child: ListTile(
        contentPadding: const EdgeInsets.all(16),
        title: Text(
          item['title'],
          style: const TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 16,
          ),
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
        ),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 8),
            Row(
              children: [
                Container(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 8,
                    vertical: 4,
                  ),
                  decoration: BoxDecoration(
                    color:
                        Theme.of(context).primaryColor.withValues(alpha: 0.1),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Text(
                    item['category'],
                    style: TextStyle(
                      color: Theme.of(context).primaryColor,
                      fontSize: 12,
                    ),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                const SizedBox(width: 12),
                Text(
                  '作者：${item['author']}',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
            const SizedBox(height: 8),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  '更新时间：${item['lastUpdated']}',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
                Row(
                  children: [
                    const Icon(
                      Icons.visibility,
                      size: 14,
                      color: Colors.grey,
                    ),
                    const SizedBox(width: 4),
                    Text(
                      item['views'].toString(),
                      style: const TextStyle(
                        fontSize: 12,
                        color: Colors.grey,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
        trailing: const Icon(Icons.arrow_forward_ios, size: 16),
        onTap: () {
          // 查看知识库详情
        },
      ),
    );
  }
}
